<template>
  <el-dialog v-model="dialogVisible" title="新增出库记录" width="600">
    <el-form
      ref="formRef"
      :model="form"
      label-width="auto"
      label-position="right"
      class="demo-form-inline"
      :rules="formRules"
      status-icon
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="仓库" prop="stashId">
            <el-select
              v-model="form.stashId"
              placeholder="请选择入库仓库"
              @change="stashChange"
            >
              <el-option
                v-for="item in stashOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="所属类别" prop="categoryId">
            <el-select
              v-model="form.categoryId"
              placeholder="请选择类别"
              @change="categoryChange"
            >
              <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物品名称" prop="name">
            <el-select v-model="form.name" placeholder="请选择类别">
              <el-option
                v-for="item in itemList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数量" prop="number">
            <el-input
              v-model="form.number"
              placeholder="请输入数量"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              placeholder="请输入备注"
              type="textarea"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" @click="submit">确定</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, nextTick, onMounted, watch } from "vue";
import { formRules } from "./rules.js";
import { addOut, setOut } from "@/api/outRecord.ts";
import { getCategory } from "@/api/category.ts";
import { getStash } from "@/api/stash.ts";
import { getItemByStashAndCategory } from "@/api/item.ts";

const dialogVisible = ref(false);

const form = ref({
  name: "",
  number: null,
  volume: null,
  categoryId: null,
  stashId: null,
  remark: null
});
const isEdit = ref();
const formRef = ref();
const categoryOptions = ref();
const stashOptions = ref();
const itemList = ref();

onMounted(() => {
  getCategoryList();
  getStashList();
});

const submit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (isEdit.value) {
        await setOut({
          ...form.value
        });
      } else {
        await addOut({
          ...form.value
        });
      }
      closeDialog();
    } else {
      console.log("error submit!", fields);
    }
  });
};
const stashChange = () => {
  if (form.value.categoryId && form.value.stashId) {
    form.value.name = "";
    getItemList();
  }
};
const categoryChange = () => {
  if (form.value.categoryId && form.value.stashId) {
    form.value.name = "";
    getItemList();
  }
};

const getCategoryList = async () => {
  const res = await getCategory({
    page: 1,
    pageSize: 100
  });
  categoryOptions.value = res.items;
};

const getStashList = async () => {
  const res = await getStash({
    page: 1,
    pageSize: 100
  });
  stashOptions.value = res.items;
};

const getItemList = async () => {
  const res = await getItemByStashAndCategory({
    stashId: form.value.stashId,
    categoryId: form.value.categoryId
  });
  console.log("resresresresres", res);
  itemList.value = res;
};

// 打开对话框
const openDialog = async (row = undefined) => {
  dialogVisible.value = true;
  await nextTick();
  formRef.value.resetFields();
  isEdit.value = row ? true : false;
  if (row) {
    form.value = { ...row };
  }
};

const emits = defineEmits(["closeDialogEvent"]);
const closeDialog = () => {
  dialogVisible.value = false;
  emits("closeDialogEvent");
};

defineExpose({
  openDialog
});
</script>

<style lang="less" scoped></style>
